package com.meixiang.dialog;

import android.annotation.TargetApi;
import android.app.Dialog;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.util.Log;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.meixiang.R;
import com.meixiang.adapter.GrildDialogAdapter;
import com.meixiang.entity.ShareList;
import com.meixiang.tool.Tool;
import com.meixiang.util.DensityUtil;
import com.nineoldandroids.view.ViewPropertyAnimator;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;

/**
 * 作者：黑哥 on 2016/7/6 15:10
 * <p/>
 * 网格布局dailog
 */
public class GrildDialogFragment extends Dialog {


    @Bind(R.id.recycler)
    RecyclerView mGrildRecycler;
    @Bind(R.id.cancel)
    TextView mCancelTextView;
    private GrildDialogAdapter mAdapter;
    private IResultListener listener;
    private Context context;
    private View view;
    private List<ShareList> shareList=new ArrayList<>();
//    private List<HashMap<String, Object>> shareList=new ArrayList<HashMap<String,Object>>();
    /**
     * @param context
     * @param listener
     */
    public GrildDialogFragment(Context context, IResultListener listener) {
        super(context);
        this.context = context;
        this.listener = listener;

        show();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        view = LayoutInflater.from(context).inflate(R.layout.dialog_grild, null);
        setContentView(view);
        super.onCreate(savedInstanceState);
        initDialog();
        initView();
        event();
    }

    private void initDialog() {
        setCancelable(false);
        getWindow().setGravity(Gravity.BOTTOM);
        getWindow().setBackgroundDrawableResource(R.color.transparent);
        int width = Tool.getWindowWith(context);
        WindowManager.LayoutParams params = getWindow().getAttributes();
        params.width = width;
        params.height = WindowManager.LayoutParams.WRAP_CONTENT;
        getWindow().setAttributes(params);
    }

    private void initView() {
        mGrildRecycler= (RecyclerView) findViewById(R.id.recycler);
        mCancelTextView= (TextView) findViewById(R.id.cancel);
        shareList.add(new ShareList(R.mipmap.share_sina,"新浪微博"));
        shareList.add(new ShareList(R.mipmap.share_qq,"QQ"));
        shareList.add(new ShareList(R.mipmap.share_qqzone,"QQ空间"));
        shareList.add(new ShareList(R.mipmap.share_wechat,"微信"));
        shareList.add(new ShareList(R.mipmap.share_moments,"朋友圈"));
        shareList.add(new ShareList(R.mipmap.share_messages,"短信"));
        shareList.add(new ShareList(R.mipmap.share_report,"举报"));

        mAdapter = new GrildDialogAdapter(context);

        mAdapter.setData(shareList);
        mGrildRecycler.setLayoutManager(new StaggeredGridLayoutManager(4, StaggeredGridLayoutManager.VERTICAL));
        mGrildRecycler.setAdapter(mAdapter);
    }

    private void event() {
        mAdapter.setOnItemClickListener(new GrildDialogAdapter.onItemClickListener() {
            @Override
            public void onItemClick(View v, int position) {
                Log.e("TAG","当前进来了");
                listener.SelectResult(position);
                dismiss();
            }
        });
        mCancelTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
    }

    public interface IResultListener {
        /**
         * 返回选中的选项下标
         *
         * @param position
         */
        void SelectResult(int position);
    }

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    @Override
    protected void onStart() {
        ViewPropertyAnimator.animate(view).translationY(DensityUtil.dip2px(context, 235)).setDuration(0).start();
        ViewPropertyAnimator.animate(view).translationY(0).setDuration(300).start();
        super.onStart();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            dismiss();
        }
        return super.onKeyDown(keyCode, event);
    }




}
